package org.wangl.jmeter.functions;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.Function;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

import java.util.*;

/**
 * @description json 字符串转换/转义
 * @author wangl
 * @date 2020-05-09
 */
@SuppressWarnings("unchecked")
public class TransfomJson extends AbstractFunction {

    private Object[] values;

    // 这个是在函数助手上显示的函数名称
    private static final String KEY = "__transform_json_wangl";

    // 该变量用来获取对参数值输入进行描述
    private static final List<String> desc = new LinkedList<String>();

    static {
        desc.add("请输入变量");
    }

    @Override
    public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {

        String encodeStr = ">>>>> 对不起主人， 超出宝宝计算能力了 >_< ";
        try {
            // 接受json字符串
            CompoundVariable json = (CompoundVariable) values[0];
            // System.out.println(">>>>> 01 " + json.getRawParameters());
            // System.out.println(json instanceof Function);
            // java 中一个\ 要用4个\
            encodeStr = json.execute().trim().replaceAll("\\\\", "");
            // System.out.println(">>>>> 02 " + encodeStr);
            encodeStr = encodeStr.replaceAll("\"", "\\\\\"");

            if (encodeStr.startsWith(",")) {
                encodeStr = encodeStr.substring(1);
            }
            System.out.println(">>>>> 请准备好你的赞美之词 ^_^ \n" + encodeStr);
        } catch (Exception e) {
            System.out.println(">>>>> error " + encodeStr);
            System.out.println(">>>>> 01 " + ((CompoundVariable) values[0]).getRawParameters());
            e.printStackTrace();
        }

        return encodeStr;
    }

    @Override
    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
        System.out.println(">>>>> length: " + parameters.size());
        checkParameterCount(parameters, parameters.size());
        Collection<CompoundVariable> params = new ArrayList<CompoundVariable>();
        Iterator<CompoundVariable> iterator = parameters.iterator();
        String code = "\\{";
        CompoundVariable c = new CompoundVariable();
        String postTransfer = "";
        while (iterator.hasNext()) {
            String split1 = iterator.next().getRawParameters();
            if (split1.indexOf("{", 2) > 0) {
                split1 = split1.replaceAll(code, "\\\\{");
            }
            postTransfer = postTransfer + "," + split1;
            // System.out.println(">>>>> set 00 " + split1);
        }
        c.setParameters(postTransfer);
        params.add(c);
        values = params.toArray();
    }

    @Override
    public String getReferenceKey() {
        return KEY;
    }

    public List<String> getArgumentDesc() {
        return desc;
    }

    public static void main(String[] args) {
        String s1 = "\\{\"success\":true,\"map\":\\{\"token\":\"abd8bad162c6458a8a267e79152dbd8f\"}\"list\":null}";
//        String result = s1.replaceAll("\"", "\\\\\"");
        System.out.println(">>>>> 2 " + s1);
    }
}